package com.jjh.common.util;

import java.util.Random;

import org.springframework.stereotype.Component;

import com.jjh.common.pojo.InfoEnum;

/**
 * 和正则表达式相关的工具类
 * 
 * @author Zjx
 */
@Component
public class RegexUtil
{
	/* 纯字母 */
	private final String numberStr = "0123456789" ;
	
	/* 纯字母 */
	private final String characterStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
	
	/**
	 * 生成一个随机字符串
	 * @param 长度
	 * @param type 0:纯数字(默认),1:纯字母,2:字母+数字
	 * @return
	 */
	public String proccedRandStr(int length,InfoEnum infoEnum)
	{
		/* 目标字符串 */
		String targetStr = this.numberStr ; 
		if(infoEnum.getCode() == InfoEnum.VAR_NUMBER.getCode())
		{
			targetStr = this.numberStr ;
		}else if(infoEnum.getCode() == InfoEnum.VAR_CHAR.getCode())
		{
			targetStr = this.characterStr ;
		}else if(infoEnum.getCode() == InfoEnum.VAR_NUMCHAR.getCode())
		{
			targetStr = this.numberStr + characterStr ; 
		}
		
		Random random = new Random();
		
		StringBuilder sb = new StringBuilder();
		/* 长度 */
		for(int i = 0 ; i < length ; i ++)
		{
			/* 随机生成一位数字 */
			int randInt = random.nextInt(targetStr.length());
			/* 随机字符 */
			char ch = targetStr.charAt(randInt);
			/* 追加 */
			sb.append(ch);
		}
		return sb.toString() ; 
	}
	
	public static void main(String[] args)
	{
		RegexUtil regexUtil = new RegexUtil();
		
		for(int i = 0 ; i < 100 ; i ++)
		{
			String tarStr = regexUtil.proccedRandStr(10, InfoEnum.VAR_NUMCHAR);
			System.out.println(i + "====>" + tarStr);
		}
	}
}
